import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;


import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;


public class Board extends JPanel implements ActionListener, Runnable {
	
	private Player p;
	private Timer timer;
	private int currentLevelIndex;
	private ArrayList<Level> levels;
	private Level currentLevel;
	private Thread animator;
	private boolean h; //at max height?
	private boolean done; //is jump done?
	private boolean k; //has jump thread started?
	
	public Board() {
		h = false;
		done = false;
		k = false;
		levels = new ArrayList<Level>();
		levels.add(new Level(new ImageIcon("Images/background.png")));
		levels.add(new Level(new ImageIcon("Images/background2.png")));
		currentLevelIndex = 0;
		currentLevel = levels.get(currentLevelIndex);
		p = new Player(this);
		
		addKeyListener(new AL());
		setFocusable(true);
		timer = new Timer(5, this);
		timer.start();
	}
	
	
	public void actionPerformed(ActionEvent e) {
		System.out.println(p.getX());
		//System.out.println(currentLevel.getEnemies().get(0).getX());
		if (endLevelCheck() && levels.size() > currentLevelIndex + 1) {
			currentLevelIndex++;
			currentLevel = levels.get(currentLevelIndex);
			p.setX(750);
			p.nx2 = 4096;
		}
		
		p.move();
		for (Enemy enemy : currentLevel.getEnemies()) {
			enemy.move();
		}
		
		repaint();
	}
	
	public void paint(Graphics g) {
		
		//if player has hit UP key and thread has not started, start the thread
		if (p.getDY() == 1 && k == false) {
			k = true;
			animator = new Thread(this);
			animator.start();
		}
		
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		
		g2d.drawImage(currentLevel.getLevelBackground(), currentLevel.getLevelBackground().getWidth(null) - p.nx2, 0, null);
		
		g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
		
		for (Enemy e : currentLevel.getEnemies()) {
			g2d.drawImage(e.getImage(), e.getX(), e.getY(), null);
		}
		
	}
	
	public boolean endLevelCheck() {
		//end of level if player has reached far right edge of level
		return (p.getX() > 1550);
	}
	
	private class AL extends KeyAdapter {
		
		public void keyReleased(KeyEvent e) {
			p.keyReleased(e);
		}
		
		public void keyPressed(KeyEvent e) {
			p.keyPressed(e);
		}
	}
	
	public Level getCurrentLevel() {
		return currentLevel;
	}
	
	public void setCurrentLevel(Level currentLevel) {
		this.currentLevel = currentLevel;
	}
	
	public Image getCurrentBackground() {
		return currentLevel.getLevelBackground();
	}

	public void run() {
		
		long beforeTime, timeDiff, sleep;
		
		beforeTime = System.currentTimeMillis();
		
		//while a jump is not completed
		while(done == false) {
			
			cycle();
			timeDiff = System.currentTimeMillis() - beforeTime;
			sleep = 10 - timeDiff;
			
			//if sleep is negative make it positive
			if (sleep < 0) {
				sleep = 2;
			}
			
			try {
				Thread.sleep(sleep);
			} catch (Exception e) {}
			
			//reset before time to current time for next jump
			beforeTime = System.currentTimeMillis();
		}
		
		done = false; //reset to false so you can jump again
		h = false; //reset to false so you can jump again
		k = false; //reset to false so you can jump again
	}
	
	
	
	public void cycle() {
		//if you're not at max height, keep increasing height
		if (h == false) {
			p.setY(p.getY() - 4);
		}
		//if you're at max height, set h to true
		if (p.getY() == 299) {
			h = true;
		}
		//if you've reached max height and you haven't hit the ground, keep decreasing height
		if (h == true && p.getY() <= 375) {
			p.setY(p.getY() + 4);
			//if you've reached the ground, set done to true
			if (p.getY() == 375) {
				done = true;
			}
		}
		
	}
	
}
